﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using NetDiskPRJ.DBUtility;
using System.Data.SqlClient;

namespace NetDiskPRJ.DAL
{
    public class Apscatcol
    {
        public Apscatcol()
        {
 
        }

        /// <summary>
        /// 获取该表需要记录的字段名
        /// <creator>zhongyl</creator>
        /// <date>2013-2-23</date>
        /// </summary>
        /// <param name="aps_tnam"></param>
        /// <returns></returns>
        public DataTable GetTrackColumn(string aps_tnam)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select aps_tnam,aps_cnam,aps_order,aps_defname,aps_usrname,aps_require,aps_def_require,aps_display,aps_default,aps_sys_default,FIsChangeTrack,aps_defhdr,aps_usrhdr ");
            strSql.Append(" FROM apscatcol  where aps_tnam='" + aps_tnam + "' AND FIsChangeTrack='Y' ");
            DataSet ds = DbHelperSQL.Query(strSql.ToString());

            return ds.Tables[0];
        }

        /// <summary>
        /// 更新数据表,根据增删改三个列表
        /// </summary>
        /// <param name="cdVisitTypeModelAddList"></param>
        /// <param name="cdVisitTypeModelDeleteList"></param>
        /// <param name="cdVisitTypeModelUpdateList"></param>
        public void UpdateByModelList(List<Model.Apscatcol> ModelUpdateList)
        {
            List<CommandInfo> cmdList = new List<CommandInfo>();
            CommandInfo cmds = new CommandInfo();
            StringBuilder strSql = new StringBuilder();

            //更新
            foreach (Model.Apscatcol cdModel in ModelUpdateList)
            {
                cmds = new CommandInfo();
                strSql = new StringBuilder();
                strSql.Append("update apscatcol set ");
                strSql.Append("aps_usrname=@aps_usrname,");
                strSql.Append("aps_usrhdr=@aps_usrhdr,");
                strSql.Append("FIsChangeTrack=@FIsChangeTrack,");
                strSql.Append("aps_require=@aps_require,");
                strSql.Append("aps_default=@aps_default");
                strSql.Append(" where aps_tnam=@aps_tnam and aps_cnam=@aps_cnam ");
                SqlParameter[] parameters = {
					new SqlParameter("@aps_usrname", SqlDbType.NVarChar,255),
					new SqlParameter("@aps_usrhdr", SqlDbType.NVarChar,255),
					new SqlParameter("@FIsChangeTrack", SqlDbType.VarChar,1),
					new SqlParameter("@aps_require", SqlDbType.Decimal,1),
					new SqlParameter("@aps_default", SqlDbType.NVarChar,255),
					new SqlParameter("@aps_tnam", SqlDbType.NVarChar,255),
					new SqlParameter("@aps_cnam", SqlDbType.NVarChar,255)};
                parameters[0].Value = cdModel.aps_usrname;
                parameters[1].Value = cdModel.aps_usrhdr;
                parameters[2].Value = cdModel.FIsChangeTrack;
                parameters[3].Value = cdModel.aps_require;
                parameters[4].Value = cdModel.aps_default;
                parameters[5].Value = cdModel.aps_tnam;
                parameters[6].Value = cdModel.aps_cnam;
                
                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters;
                cmdList.Add(cmds);
            }

            if (cmdList.Count > 0)
            {
                DbHelperSQL.ExecuteSqlTran(cmdList);
            }

        }

        /// <summary>
        /// 获得数据列表
        ///<creator>zhongyl</creator>
        /// <date>2012-02-21</date>
        /// </summary>
        public DataSet GetListds()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select aps_tnam,aps_cnam,aps_order,aps_defname,aps_usrname,aps_require,aps_def_require,aps_display,aps_default,aps_sys_default,FIsChangeTrack,aps_defhdr,aps_usrhdr  FROM apscatcol ");
            strSql.Append(" ORDER BY aps_tnam");
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public NetDiskPRJ.Model.Apscatcol DataRowToModel(DataRow row)
        {
            NetDiskPRJ.Model.Apscatcol model = new NetDiskPRJ.Model.Apscatcol();
            if (row != null)
            {
                if (row["aps_tnam"] != null)
                {
                    model.aps_tnam = row["aps_tnam"].ToString();
                }
                if (row["aps_cnam"] != null)
                {
                    model.aps_cnam = row["aps_cnam"].ToString();
                }
                if (row["aps_order"] != null && row["aps_order"].ToString() != "")
                {
                    model.aps_order = int.Parse(row["aps_order"].ToString());
                }
                if (row["aps_defname"] != null)
                {
                    model.aps_defname = row["aps_defname"].ToString();
                }
                if (row["aps_usrname"] != null)
                {
                    model.aps_usrname = row["aps_usrname"].ToString();
                }
                if (row["aps_require"] != null && row["aps_require"].ToString() != "")
                {
                    model.aps_require = decimal.Parse(row["aps_require"].ToString());
                }
                if (row["aps_def_require"] != null && row["aps_def_require"].ToString() != "")
                {
                    model.aps_def_require = decimal.Parse(row["aps_def_require"].ToString());
                }
                if (row["aps_display"] != null && row["aps_display"].ToString() != "")
                {
                    model.aps_display = decimal.Parse(row["aps_display"].ToString());
                }
                if (row["aps_default"] != null)
                {
                    model.aps_default = row["aps_default"].ToString();
                }
                if (row["aps_sys_default"] != null && row["aps_sys_default"].ToString() != "")
                {
                    model.aps_sys_default = decimal.Parse(row["aps_sys_default"].ToString());
                }
                if (row["FIsChangeTrack"] != null)
                {
                    model.FIsChangeTrack = row["FIsChangeTrack"].ToString();
                }
                if (row["aps_defhdr"] != null)
                {
                    model.aps_defhdr = row["aps_defhdr"].ToString();
                }
                if (row["aps_usrhdr"] != null)
                {
                    model.aps_usrhdr = row["aps_usrhdr"].ToString();
                }
            }
            return model;
        }

    }
}
